'use strict';

angular.module('Initialize').controller('WorkLocationBindController', ['$scope', '$state', 'PublicModule', 'PublicEnum', 'PublicFunction', '$location', 'Authentication',
    function($scope, $state, PublicModule, PublicEnum, PublicFunction, $location, Authentication) {
        $scope.btnSave = function(){
            //收集当前作业的绑定记录
            if(oldWorkFocusedRecord != null){
                var stationWorkRecords = $scope.Module.StationWork.CacheRecords;
                for(var i=0; i<stationWorkRecords.length; i++){
                    if(stationWorkRecords[i].Work === oldWorkFocusedRecord._id){
                        $scope.Module.StationWork.removeRecord(stationWorkRecords[i]);
                        i--;
                    }
                }
                $scope.Module.StationWork.Records = $scope.Module.StationWork.CacheRecords.concat($scope.Module._StationWork.CacheRecords);
            }

            for(var i=0; i<$scope.Module.StationWork.Records.length; i++){
                if($scope.Module.StationWork.Records[i].WorkLocation._id == undefined){
                    PublicFunction.alert('数据错误', '工位不能为空');
                    return;
                }
            }
            $scope.Module.StationWork.save(function(){
                $scope.Module.StationWork.Records = [];
                $scope.Module._StationWork.Records = [];
                $scope.Module._StationWork.emitOnLoadEvent();
                $scope.Module.Work.load();
            });
        };
        $scope.btnCancel = function(){
            $state.reload();
        };
        var oldWorkFocusedRecord = null;
        $scope.rowChange = function(){
            //收集当前作业的绑定记录
            if(oldWorkFocusedRecord != null){
                var stationWorkRecords = $scope.Module.StationWork.CacheRecords;
                for(var i=0; i<stationWorkRecords.length; i++){
                    if(stationWorkRecords[i].Work === oldWorkFocusedRecord._id){
                        $scope.Module.StationWork.removeRecord(stationWorkRecords[i]);
                        i--;
                    }
                }
                $scope.Module.StationWork.Records = $scope.Module.StationWork.CacheRecords.concat($scope.Module._StationWork.CacheRecords);
            }

            oldWorkFocusedRecord = $scope.Module.Work.FocusedRecord;
            $scope.Module._StationWork.Records = [];
            $scope.Module._StationWork.FocusedRecord = null;

            //载入当前作业的绑定记录
            if($scope.Module.Work.FocusedRecord.IsLoadStationWork){
                var _StationWorkRecords = [];
                var stationWorkRecords = $scope.Module.StationWork.Records;

                for(var i=0; i<stationWorkRecords.length; i++){
                    if(stationWorkRecords[i].Work === $scope.Module.Work.FocusedRecord._id){
                        _StationWorkRecords.push(stationWorkRecords[i]);
                    }
                }
                $scope.Module._StationWork.Records = _StationWorkRecords;  
                $scope.Module._StationWork.emitOnLoadEvent();
            }
            else{
                $scope.Module.StationWork.find({Work: $scope.Module.Work.FocusedRecord._id}, null, $scope.Module.StationWork.Populate, function(records){
                    $scope.Module.Work.FocusedRecord.IsLoadStationWork = true;
                    for(var i=0; i<records.length; i++){
                        if(records[i].WorkLocation.Department != null){
                            records[i].DepartmentName = records[i].WorkLocation.Department.Name;
                        }
                    }
                    $scope.Module._StationWork.Records = records;
                    $scope.Module.StationWork.Records = $scope.Module.StationWork.CacheRecords.concat(records);
                    $scope.Module._StationWork.emitOnLoadEvent();
                });
            }
        };

        $scope.addRow = function(record){
            record.Work = $scope.Module.Work.FocusedRecord._id;
        };

        $scope.cellValueChange = function(columnName, newValue, oldValue){
            if(columnName === "WorkLocation.Code"){
                if(newValue.length > 0){
                    $scope.Module._StationWork.FocusedRecord.DepartmentName = newValue[0].Department.Name;
                }
                else{
                    $scope.Module._StationWork.FocusedRecord.DepartmentName = '';
                }
                $scope.Module._StationWork.emitRowsDataChangeEvent();
            }
        };

        $scope.refBeforeShow = function(refScope) {
            var workLocationIds = [];
            for (var i = 0; i < $scope.Module._StationWork.Records.length; i++) {
                if ($scope.Module._StationWork.Records[i].WorkLocation !== undefined && $scope.Module._StationWork.Records[i].WorkLocation !== "") {
                    workLocationIds.push($scope.Module._StationWork.Records[i].WorkLocation._id);
                }
            }
            refScope.Module.Condition = {
                _id: {
                    $nin: workLocationIds
                },
                IsEffective: true
            };
        };

        PublicModule.initModule($scope, 'Work');
        $scope.Module.Work.LoadMode = PublicEnum.LoadMode.CacheAndPaging;
        $scope.Module.Work.PageSize = 15;

    	PublicModule.initModule($scope, 'StationWork');
        $scope.Module.StationWork.Populate = {path: 'WorkLocation', select: 'Code Name Department', populate: {
                path: 'Department',
                select: 'Name',
                model: 'Department'
            }
        };
        $scope.Module.StationWork.LoadMode = PublicEnum.LoadMode.CacheAndNoPaging;
        $scope.Module.StationWork.PageSize = 15;

        PublicModule.initModule($scope, '_StationWork');
        $scope.Module._StationWork.LoadMode = PublicEnum.LoadMode.CacheAndNoPaging;
        $scope.Module._StationWork.PageSize = 15;
    }
]);